package com.wisezone.springmvc.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 重定向传参 1.手动拼接URL参数； 如果url地址有中文，需手动编解码 redirect:/two1?message=小明"
 * URLEncoder.encode("小明", "UTF-8"); URLDecoder.decode(message, "utf-8");
 * 2.自动拼接URL参数 Model model.addAttribute("message", "张三"); RedirectAttributes
 * addFlashAttribute session对象 addAttribute 拼接url后,requet对象
 * 
 * 3. @SessionAttributes(names= {"message"}) 1.用在类头;
 * 2.当检查到model中保存有对应的属性值会自动保存一份到session对象中
 */
@Controller
@RequestMapping("/")
@SessionAttributes(names = { "message" })
public class ParameterController {
	// ========================手动拼接URL参数=============
	@RequestMapping("/one")
	public String parmetOne() throws UnsupportedEncodingException {
		System.out.println("paretOne>>>");
		String name = URLEncoder.encode("小明", "UTF-8");
		return "redirect:/two?message=" + name; // 重定向
	}

	@RequestMapping("/two")
	public String parmetTwo(String message) throws UnsupportedEncodingException {
		String msg = URLDecoder.decode(message, "utf-8");
		System.out.println("paretTwo>>> msg :" + msg);
		return "success";
	}

	// =====================自动拼接URL参数 ==================
	@RequestMapping("/auto_one")
	public String parmetAutoOne(Model model, RedirectAttributes redirectAttr) {
		model.addAttribute("message", "张三");
		model.addAttribute("password", "123");
		redirectAttr.addAttribute("message", "张三");
		return "redirect:/auto_two"; // 重定向
	}

	@RequestMapping("/auto_two")
	public String parmetAutoTwo(String message) {
		System.out.println("paretTwo>>> msg :" + message);
		return "success";
	}

	// ==============================将数据暂存到session对象中，请求结束从session对象中销毁=====
	@RequestMapping("/auto_redirect_one")
	public String parmetAutoRedirectOne(RedirectAttributes redirectAttr) {
		redirectAttr.addFlashAttribute("message", "张三"); // 将数据暂存到session对象中，请求结束从session对象中销毁
		return "redirect:/auto_redirect_two"; // 重定向
	}

	@RequestMapping("/auto_redirect_two")
	public String parmetAutoRedirectTwo(@ModelAttribute("message") String message) {
		System.out.println("paretTwo>>> msg :" + message);
		return "success";
	}

	// =================@SessionAttributes====================
	@RequestMapping(value = "/sessionattr")
	public String sessionAttruts(Model model) {
		model.addAttribute("message", "session对象注解学习");
		return "success";
	}

	@RequestMapping(value = "/sessionattrtwo")
	public String sessionTwo(@ModelAttribute("message") String msg) {
		System.out.println("sessionTwo :" + msg);
		return "success_two";
	}
	// http://ip:port/springmvc/user/delete?userid=2
	// http://ip:port/springmvc/user/delete/2    resetfull风格编程
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public String deleteUserRest(@PathVariable("id") int userId, Model model) {
		System.out.println("删除项ID :" + userId);
		model.addAttribute("message", "删除id="+userId+"记录成功");
		return "success";
	}

}
